package reflect;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Scanner;

public class ReflectDemo4 {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        System.out.println("原始方法调用方法");
        Person p=new Person();
        p.sayHello();


//        System.out.println("反射机制调用方法");
//        Class c=Class.forName("reflect.Person");
//        Object o = c.newInstance();
//        //调用方法sayHello()
//        Method method=c.getMethod("sayHello");
//        method.invoke(o);//o.sayHello();

        System.out.println("反射机制调用方法");
        Scanner scan=new Scanner(System.in);
        System.out.println("请输入一个类名：");
        String className = scan.nextLine();
        System.out.println("请输入调用的方法：");
        String methodName=scan.nextLine();

        Class c=Class.forName(className);
        Object o = c.newInstance();
        Method method=c.getMethod(methodName);
        method.invoke(o);
    }
}

